function hideNewIssueProjectFilters() {
    dojo.style(dojo.query(".filter", "filterGroupnewissuemodule")[0], "opacity", "0.5");
    dojo.style(dojo.query(".filter", "filterGroupnewissueversion")[1], "opacity", "0.5");
}

function clearNewIssueForm() {
    dijit.byId("typenewissue").reset();
    dijit.byId("severitynewissue").reset();
    dijit.byId("summarynewissue").reset();
    dijit.byId("newissuetextarea").setValue("");
    dojo.byId("parentModulenewissuemodule").value = "";
    dojo.byId("affectedVersionnewissueversion").value = "";
    dijit.byId("parentModulenewissuemodule0").reset();
    dijit.byId("affectedVersionnewissueversion0").reset();
}

function refreshNewIssueLocatorStores(alias, prj) {
    clearNewIssueForm();
    hideNewIssueProjectFilters();
    dojo.byId("newissueproject").value = prj;
    dojo.byId("newissueprojectalias").value = alias;
    byId("issueValidationWarningContainer").innerHTML = "";
    storeGridnewissuemodule.initial = true;
    storeGridnewissuemodule.filters = [];
    storeGridnewissuemodule.filters.push({
        from: prj,
        range: false,
        type: "ENTITY",
        path: "parentProject.id"
    });
    entityFilter('newissuemodule', 'DefaultModule');
    storeGridnewissueversion.initial = true;
    storeGridnewissueversion.filters = [];
    storeGridnewissueversion.filters.push({
        from: prj,
        range: false,
        type: "ENTITY",
        path: "parentProject.id"
    });
    entityFilter('newissueversion', 'DefaultVersion');
    fireStoreFetchQueue();
}

function changeIssueType(type) {
    var src = contextPath + "/resource.iwebmvc?file=/images/common/__" + currentTheme + ".gif" + sessionQueryPath;
    var replace = "bug_";
    switch (Number(type)) {
        case 1: replace = "enhancement_"; break;
        case 2: replace = "feedback_"; break;
        case 3: replace = "task_"; break;
    }
    dojo.byId("newissuecreateimg").src = src.replace("__", replace);
}

function createIssue(alias, projectClass, issueClass, moduleClass, versionClass) {
    byId("issueValidationWarningContainer").innerHTML = "";
    dojo.style(byId("issueNewLoadingContainer"), "display", "");
    dijit.byId("newissuecreate").setDisabled(true);
    var type = dijit.byId("typenewissue").getValue() || 0;
    var severity = dijit.byId("severitynewissue").getValue() || 0;
    var prj = dwr.engine.remote.newObject(projectClass, {
        id: dojo.byId("newissueproject").value
    });
    var issue = dwr.engine.remote.newObject(issueClass, {
        type: type,
        severity: severity,
        parentProject: prj,
        parentModule: obtainNewIssueModule(moduleClass, prj),
        affectedVersion: obtainNewIssueAffectedVersion(versionClass, prj),
        summary: dijit.byId("summarynewissue").getValue(),
        description: dijit.byId("newissuetextarea").getValue()
    });
    RemoteIssueManager.createIssue(issue, {
        callback: function (id) {
            dijit.byId("newissuecreate").setDisabled(false);
            dojo.style(byId("issueNewLoadingContainer"), "display", "none");
            var gridProjectIssues = dijit.byId("grid" + alias + "Issues");
            if (gridProjectIssues) {
                gridProjectIssues.store.initial = true;
                entityFilter(alias + "Issues", issueClass);
            }
            closePopup("newissueform");
            refreshAllActivities(alias);
        },
        errorHandler: function(errorString, exception) {
            dijit.byId("newissuecreate").setDisabled(false);
            byId("issueValidationWarningContainer").innerHTML = errorString ? errorString : exception;
            dojo.style(byId("issueNewLoadingContainer"), "display", "none");
        }
    });
}

function obtainNewIssueModule(moduleClass, project) {
    var module = dojo.byId("parentModulenewissuemodule").value;
    return module ? dwr.engine.remote.newObject(moduleClass, {
        id: module,
        parentProject: project,
        parentModule: null
    }) : null;
}

function obtainNewIssueAffectedVersion(versionClass, project) {
    var version = dojo.byId("affectedVersionnewissueversion").value;
    return version ? dwr.engine.remote.newObject(versionClass, {
        id: version,
        parentProject: project,
        parentVersion: null
    }) : null;
}

function voteIssue(issueClass, id, alias, current) {
    RemoteIssueManager.vote(dwr.engine.remote.newObject(issueClass, {id: id}), function (result) {
        dijit.byId(alias + "Vote").setDisabled(true);
        if (result) {
            byId(alias + "Votes").innerHTML = current + 1;
            refreshAllActivities(alias);
        }
    });
}

function watchIssue(issueClass, id, alias, current) {
    RemoteIssueManager.watch(dwr.engine.remote.newObject(issueClass, {id: id}), function (result) {
        dijit.byId(alias + "Watch").setDisabled(true);
        if (result) {
            byId(alias + "Watchers").innerHTML = current + 1;
            refreshAllActivities(alias);
        }
    });
}

function commentIssue(issueClass, commentClass, id, parent, chat, alias, comments, gravatar, author, on) {
    dijit.byId("newCommentButton").setDisabled(true);
    var loading = byId("issueCommentLoadingContainer");
    dojo.style(loading, "display", "inline");
    RemoteIssueManager.comment(dwr.engine.remote.newObject(issueClass, {id: id}), parent ? dwr.engine.remote.newObject(commentClass, {id: parent}) : null, chat, function () {
        dojo.style(loading, "display", "none");
        dijit.byId("newCommentButton").setDisabled(false);
        closePopup("newcommentform");
        refreshAllActivities(alias);
        var calias = alias.replace(/-/g, "_");
        var tab = dijit.byId(calias + "Control");
        var commentsTab = isTabOpen(tab, comments);
        tab.selectChild(commentsTab);
        var maindiv = document.createElement("div");
        var leftdiv = document.createElement("div");
        var img = new Image();
        img.src = gravatar;
        leftdiv.appendChild(img);
        maindiv.appendChild(leftdiv);
        var rightdiv = document.createElement("div");
        var label = document.createElement("div");
        label.innerHTML = author + "&nbsp;" + on + "&nbsp;" + new Date().toString().substring(0, 24);
        rightdiv.appendChild(label);
        var comm = document.createElement("div");
        comm.innerHTML = chat;
        rightdiv.appendChild(comm);
        maindiv.appendChild(rightdiv);
        commentsTab.domNode.appendChild(maindiv);
        var sepdiv = document.createElement("div");
        var sep = document.createElement("hr");
        sepdiv.appendChild(sep);
        commentsTab.domNode.appendChild(sepdiv);
        dojo.style(maindiv, {width: "97%", padding: "5px", textAlign: "left"});
        dojo.addClass(maindiv, "overclear");
        dojo.style(leftdiv, "width", "12%");
        dojo.addClass(leftdiv, "float_left");
        dojo.style(rightdiv, "width", "88%");
        dojo.addClass(rightdiv, "float_right");
        dojo.addClass(label, "label");
        dojo.style(sepdiv, "marginBottom", "4px");
        dojo.style(sep, "height", "2px");
        dojo.addClass(sep, "color_" + currentTheme);
    });
}

function attachFileToIssue(issueClass, id, doc, alias, screenshots, files) {
    var button = dijit.byId("newAttachmentButton");
    button.setDisabled(true);
    var loading = byId("issueAttachmentLoadingContainer");
    dojo.style(loading, "display", "inline");
    RemoteIssueManager.addAttachment(dwr.engine.remote.newObject(issueClass, {id: id}), dwr.engine.remote.newObject("Document", {id: doc}), function(data) {
        button.setDisabled(false);
        dojo.style(loading, "display", "none");
        closePopup("newattachmentform");
        refreshAllActivities(alias);
        if (data.text || data.image) {
            var calias = alias.replace(/-/g, "_");
            var tab = dijit.byId(calias + "Control");
            var divimg = document.createElement("div");
            var img = new Image();
            divimg.appendChild(img);
            var divid = document.createElement("div");
            divid.appendChild(document.createTextNode(data.identifier));
            dojo.style(divid, "fontWeight", "bold");
            if (data.image) {
                var td = document.createElement("td");
                img.src = contextPath + "resource.iwebmvc?file=" + data.id + "&width=200&height=200" + sessionQueryPath;
                td.appendChild(divimg);
                td.appendChild(divid);
                byId(calias + "Screenshots").appendChild(td);
                dojo.style(divid, "fontWeight", "bold");
                dojo.style(td, {width: "220px", border: "1px solid gray", padding: "5px", textAlign: "center"});
                tab.selectChild(isTabOpen(tab, screenshots));
                dojo.connect(img, "onclick", (function(thedata) {
                    return function() {
                        lightbox(contextPath + 'resource.iwebmvc?file=' + thedata.id + sessionQueryPath);
                    };
                })(data));
            } else if (data.text) {
                var span = document.createElement("span");
                img.src = contextPath + "resource.iwebmvc?file=/images/filetypes/" + data.extension + "_" + currentTheme + ".png" + sessionQueryPath;
                span.appendChild(divimg);
                span.appendChild(divid);
                var attachments = isTabOpen(tab, files);
                attachments.domNode.appendChild(span);
                dojo.style(span, {display: "inline-block", width: "80px", marginRight: "3px"});
                dojo.style(divimg, {textAlign: "center"});
                dojo.style(divid, {overflow: "hidden", textOverflow: "ellipsis", textAlign: "center", height: "15px"});
                tab.selectChild(attachments);
                dojo.connect(img, "onclick", (function(thedata) {
                    return function() {
                        previewTextFile(byId('textPreviewerContainer'), thedata.extension, contextPath + 'resource.iwebmvc?file=' + thedata.id + sessionQueryPath + '&escape=true');
                    };
                })(data));
            }
        }
    });
}

function assignIssueToMe(issueClass, alias, id, imgUrl) {
    var button = dijit.byId(alias + "Assign");
    button.setDisabled(true);
    var loading = byId(alias + "AssignLoading");
    dojo.style(loading, "display", "inline");
    RemoteIssueManager.assignToMe(dwr.engine.remote.newObject(issueClass, {id: id}), function(data) {
        dojo.style(loading, "display", "none");
        byId(alias + "StatusImage").src = imgUrl;
        byId(alias + "StatusContainer").innerHTML = data;
        var current = byId("panel" + alias + "CurrentAssign");
        if (current) {
            dojo.style(current, "display", "none");
        }
        dojo.style(byId("panel" + alias + "MyAssign"), "display", "block");
    });
}

function cleanAndOpenLinkIssueForm(issueClass, issue, project, alias) {
    byId("linkParentIssue").value = issue;
    byId("linkParentIssueAlias").value = alias;
    byId("linkParentProject").value = project;
    byId("targetnewlink").value = "";
    dijit.byId("typenewlink").reset();
    dijit.byId("targetnewlink0").reset();
    dijit.byId("gridnewlinkLocatorGrid").store.filters=[{
        from: project,
        range: false,
        type: "ENTITY",
        path: "parentProject.id"
    }];
    entityFilter("newlinkLocatorGrid", issueClass);
    openPopup("linkIssuesForm");
}

function linkIssue(issueClass, id, linked, type, alias, links, text, other, linkedAlias, typeText) {
    var button = dijit.byId("newIssueLinkButton");
    button.setDisabled(true);
    var loading = byId("issueLinkLoadingContainer");
    dojo.style(loading, "display", "inline");
    RemoteIssueManager.linkWith(dwr.engine.remote.newObject(issueClass, {id: id}), dwr.engine.remote.newObject(issueClass, {id: linked}), type, function(data) {
        button.setDisabled(false);
        dojo.style(loading, "display", "none");
        if (data) {
            byId("issueLinkValidationWarningContainer").innerHTML = data;
        } else {
            closePopup("linkIssuesForm");
            refreshAllActivities(alias);
            var calias = alias.replace(/-/g, "_");
            var tab = dijit.byId(calias + "Control");
            var linkTab = isTabOpen(tab, links);
            tab.selectChild(linkTab);
            var main = document.createElement("div");
            var span = document.createElement("span");
            span.innerHTML = text.substring(0, text.length - 3) + "(" + alias + ")&nbsp;";
            main.appendChild(span);
            var span2 = document.createElement("span");
            span2.innerHTML = typeText + "&nbsp;";
            main.appendChild(span2);
            var span3 = document.createElement("span");
            span3.innerHTML = other + "&nbsp;<a href='javascript:openOrSelectTab(\"" + linked + "\", \"" + linkedAlias + "\", \"issue\")'>" + linkedAlias + "</a>";
            main.appendChild(span3);
            linkTab.domNode.appendChild(main);
            dojo.style(main, "marginBottom", "5px");
        }
    });
}

function cleanAndOpenCloseIssueForm(alias, issue) {
    byId("issueToClose").value = issue;
    byId("issueToCloseAlias").value = alias;
    dijit.byId("resolutionStatuscloseIssue").reset();
    dijit.byId("closeIssueCommentTextarea").setValue("");
    openPopup("closeIssueForm");
}

function closeIssue(issueClass, alias, id, status, statusLabel, comment) {
    var error = byId("closeIssueValidationWarningContainer");
    error.innerHTML = "";
    var button = dijit.byId("closeIssueButton");
    button.setDisabled(true);
    var loading = byId("closeIssueLoadingContainer");
    dojo.style(loading, "display", "inline");
    RemoteIssueManager.closeIssue(dwr.engine.remote.newObject(issueClass, {id: id}), status, comment, function(data) {
        button.setDisabled(false);
        dojo.style(loading, "display", "none");
        if (data) {
            error.innerHTML = data;
        } else {
            refreshAllActivities(alias);
            closePopup("closeIssueForm");
            dijit.byId(alias + "Link").setDisabled(true);
            dijit.byId(alias + "Vote").setDisabled(true);
            dijit.byId(alias + "Watch").setDisabled(true);
            dijit.byId(alias + "Assign").setDisabled(true);
            dijit.byId(alias + "Resolve").setDisabled(true);
            dijit.byId(alias + "Comment").setDisabled(true);
            dijit.byId(alias + "Attachment").setDisabled(true);

            dijit.byId(alias + "TargetVersion").setDisabled(true);
            byId(alias + "StatusImage").src = contextPath + "resource.iwebmvc?file=/images/common/closed_" + currentTheme + ".gif" + sessionQueryPath;
            byId(alias + "ResolutionStatusImage").src = contextPath + "resource.iwebmvc?file=/images/common/fixed_" + currentTheme + ".gif" + sessionQueryPath;
            byId(alias + "Status").innerHTML = "";
            byId(alias + "ResolutionStatus").innerHTML = statusLabel;
        }
    });
}

function openTargetVersionForm(id, alias, projectAlias) {
    dijit.byId("targetVersionButton").setDisabled(true);
    byId("targetIssueId").value = id;
    byId("targetVersionId").value = "";
    byId("targetVersionAlias").value = alias;
    byId("targetVersionTree").value = projectAlias + "VersionTree";
    byId("targetVersionTreeContainer").value = projectAlias + "VersionTreeContainer";
    byId("targetVersionContainer").appendChild(dijit.byId(projectAlias + "VersionTree").domNode);
    byId("targetVersionValidationWarningContainer").innerHTML = "";
    openPopup("targetversion");
}

function placebackTargetVersionSelector() {
    var container = byId(byId("targetVersionTreeContainer").value);
    var tree = dijit.byId(dojo.byId("targetVersionTree").value);
    container.appendChild(tree.domNode);
}